<template>
  <view class="gift-record-page">
    <!-- 列表内容区域 -->
    <view class="record-container">
      <scroll-view 
        class="record-scroll"
        scroll-y="true"
        refresher-enabled="true"
        refresher-threshold="80"
        refresher-default-style="black"
        @refresherpulling="onPulling"
        @refresherrefresh="onRefresh"
        @scrolltolower="onLoadMore"
      >
        <!-- 记录列表 -->
        <view class="record-list">
          <view 
            class="record-item" 
            v-for="(record, index) in records" 
            :key="record.id"
          >
            <view class="record-info" v-for="(k,v) in record.info" :key="index">
              <view class="icon">
					<image :src="k.thumb" mode="aspectFill"></image>
              </view>
			  <view class="r">
			  	<view class="record-title">
			  	  <view class="gift-name">{{ k.title }}</view>
			  	  <view class="gift-time">{{ record.add_time }}</view>
			  	</view>
			  	<view class="record-details">
			  	  <text>数量: {{ k.num }}个</text>
			  	</view>
			  </view>
            </view>
           <view class="tool">
			   <view class="zhuangtai">
					当前状态： 
					<span  v-if="record.order_status==3" style="color: blue;">已领取</span>
					<span v-else style="color: red;">未领取</span>
			   </view>
				<!-- 删除按钮 -->
				<view class="delete-btn" @click="deleteRecord(index, record.id)">
				  <uni-icons type="trash" size="18" color="#ff3b30"></uni-icons>
				</view>
           </view>
          </view>
          
          <!-- 空状态 -->
          <view class="empty-state" v-if="records.length === 0 && !loading">
            <uni-icons type="empty" size="60" color="#ccc"></uni-icons>
            <text class="empty-text">暂无赠送记录</text>
          </view>
          
          <!-- 加载中 -->
          <view class="loading-state" v-if="loading">
            <uni-loading-icon size="24" color="#777"></uni-loading-icon>
            <text class="loading-text">加载中...</text>
          </view>
          
          <!-- 没有更多 -->
          <view class="no-more" v-if="!hasMore && records.length > 0 && !loading">
            <text>没有更多记录了</text>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js'
export default {
  data() {
    return {
      // 记录数据
      records: [],
      // 分页参数
      page: 1,
      pageSize: 10,
      total: 0,
      // 状态标识
      loading: false,
      hasMore: true,
      // 状态映射表
      statusMap: {
        1: { text: '已赠送', class: 'status-success' },
        2: { text: '已过期', class: 'status-expired' },
        3: { text: '已撤回', class: 'status-canceled' }
      }
    };
  },
  onLoad() {
    // 页面加载时获取数据
    this.loading = true;
    this.fetchRecords();
  },
  methods: {
	  getUserInfo() {
	  	request({
	  		url: 'order/share-list',
	  		method: 'get',
	  		data: {
				page:this.page
	  		}
	  	}).then(res => {
	  		console.log(res)
	  		
	  	})
	  
	  },
    // 获取记录数据
    fetchRecords() {
      // 模拟网络请求
        // 模拟数据
       
	  request({
	  	url: 'order/share-list',
	  	method: 'get',
	  	data: {
	  		page:this.page
	  	}
	  }).then(res => {
	  	console.log(res)
		let mockData = res.data.list
		// 处理状态文本和样式
		const processedData = mockData.map(item => ({
		  ...item,
		}));
		
		// 第一页清空数据，后续页面追加数据
		if (this.page === 1) {
		  this.records = processedData;
		} else {
		  this.records = [...this.records, ...processedData];
		}
		
		// 模拟总条数，控制是否还有更多数据
		this.total = 20;
		this.hasMore = this.records.length < this.total;
		
		// 结束加载状态
		this.loading = false;
	  	
	  })
    },
    
    // 下拉刷新相关
    onPulling() {
      // 下拉过程中可以显示一些提示
    },
    
    onRefresh() {
      // 重置分页，重新加载第一页
      this.page = 1;
      this.fetchRecords();
    },
    
    // 上拉加载更多
    onLoadMore() {
      if (!this.loading && this.hasMore) {
        this.loading = true;
        this.page++;
        this.fetchRecords();
      }
    },
    
    // 删除记录
    deleteRecord(index, id) {
		let that = this
      uni.showModal({
        title: '删除确认',
        content: '确定要删除这条记录吗？',
        success: (res) => {
          if (res.confirm) {
            // 从数组中删除对应记录
            
			request({
				url: 'order/order-update',
				method: 'post',
				data: {
					oid: id,
					status:5
				}
			}).then(res => {
				that.records = that.records.filter(record => record.id !== id);
				uni.showToast({
				  title: '删除成功',
				  icon: 'none',
				  duration: 1500
				});
				that.fetchRecords();
				
			})
         
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.gift-record-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 标题样式 */
.page-title {
  height: 44px;
  line-height: 44px;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}

/* 滚动区域 */
.record-container {
  height: 100vh;
}

.record-scroll {
  width: 100%;
  height: 100%;
}

/* 列表样式 */
.record-list {
  padding: 10px;
}

.record-item {
  align-items: center;
  padding: 12px;
  margin-bottom: 10px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.record-info {
	display: flex;
	align-items: center;
	border-bottom: 1px solid #eee;
	padding: 20rpx 0;
	.icon{
		width: 120rpx;
		height: 120rpx;
		border-radius: 10rpx;
		overflow: hidden;
		margin-right: 20rpx;
		image{
			display: block;
			height: 100%;
			width: 100%;
		}
	}
}

.record-title {
  margin-bottom: 8px;
}

.gift-name {
  font-size:28rpx;
  font-weight: 500;
  color: #333;
}

.gift-time {
  font-size: 12px;
  color: #999;
}

.record-details {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: #666;
  margin-bottom: 6px;
}

.record-status {
  display: inline-block;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
}

.status-success {
  background-color: #e5f3ea;
  color: #007e33;
}

.status-expired {
  background-color: #f8f8f8;
  color: #999;
}

.status-canceled {
  background-color: #fef0f0;
  color: #cc0000;
}

/* 删除按钮 */
.delete-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  color: #ccc;
}

.empty-text {
  margin-top: 16px;
  font-size: 14px;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
  color: #777;
}

.loading-text {
  margin-top: 8px;
  font-size: 14px;
}

/* 没有更多 */
.no-more {
  text-align: center;
  padding: 20px 0;
  color: #999;
  font-size: 14px;
}
.tool{
	display: flex;
	align-items: center;
	justify-content: space-between;
	.zhuangtai{
		font-size: 28rpx;
	}
}
</style>
    